﻿#region Version Info
/* ========================================================================
* 【本类功能概述】
* 
* 作者：李荣楠
* 时间：2013-11-5 14:09:27
* 文件名：Class1
* .net版本：4.0 
* ========================================================================
*/
#endregion
using System;
using System.Data;
using System.Data.SqlClient;
using LCL.Config;
using LCL.Entity;
using LCL.Factory;

namespace LCL.Data
{
    /// <summary>
    /// 关于用户的操作
    /// </summary>
    public partial class DataProvider : IDataProvider
    {
        /// <summary>
        /// 检查登录，判断登录状态
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="password">密码</param>
        /// <param name="IsAdministrator">是否是管理员</param>
        /// <returns>登录状态</returns>
        public EnumLoginState CheckUser(int userId, string password, bool IsAdministrator)
        {
            string pwd = string.Empty;
            EnumUserState us = EnumUserState.Lock;
            bool exist = false;
            string AdminSTR = string.Empty;
            if (IsAdministrator)
            {
                AdminSTR = " and (RGP_Groups.GroupType=1)";
            }

            string Sql = "select islock, pwd,state from users,RGP_Groups where id=" + userId + AdminSTR;
            IDataReader rd = SqlHelper.ExecuteReader(Sql);
            if (rd.Read())
            {
                pwd = rd["pwd"].ToString();
                us = (EnumUserState)Convert.ToInt16(rd["state"]);
                exist = true;
                ////lNum = Convert.ToInt32(rd["LoginTimes"]);
            }
            rd.Close();
            if (!exist || pwd == string.Empty || pwd != password)
            {
                return EnumLoginState.Err_NameOrPwdError;
            }
            if (us == EnumUserState.Register)
            {
                //if (UiConfig.RegVer == "1")//是否启用电子邮件激活
                //{
                //    return EnumLoginState.Err_UnActivation;
                //}
            }
            else if (us == EnumUserState.Lock)
            {
                if (IsAdministrator)
                {
                    return EnumLoginState.Err_AdminLocked;
                }
                return EnumLoginState.Err_Locked;
            }
            return EnumLoginState.Succeed;
        }
        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool RegUser(string username, string pwd, string email)
        {
            SqlParameter a = new SqlParameter("",SqlDbType.BigInt);
            string sqlstr = "insert into users (name,pwd,email) values ('" + username + "','" + pwd + "','" + email + "')";
            if (SqlHelper.ExecuteNonQuery(sqlstr) == 1)
            {
                return true;
            }
            else
            {
                return  false;
            }
        }
        /// <summary>
        /// 开始登陆
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public bool Login(ref string uid,string username, string pwd, bool IsAdministrator)
        {
            string sql = "select id from users where name='" + username + "' and pwd='" + pwd + "'";
            if (SqlHelper.ExecuteScalar(sql)!=null)
            {
                uid = SqlHelper.ExecuteScalar(sql).ToString();
                return true;
            }
            return false;
        }
    }
}
